package com.formiaomiao.demain.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.fragment.UpdateRefreshFragment;
import com.formiaomiao.demain.util.GlideCacheUtil;
import com.formiaomiao.demain.util.MessageEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class SettingsActivity extends BaseActivity {

    @BindView(R.id.iv_class_back)
    ImageView ivClassBack;
    @BindView(R.id.tv_class_name)
    TextView tvClassName;
    @BindView(R.id.tv_modify)
    TextView tvModify;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.rl_clear)
    RelativeLayout rlClear;

    UpdateRefreshFragment updateRefreshFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        ButterKnife.bind(this);
        ViewInit();
    }

    public void ViewInit() {
        tvClassName.setText("设置");
        tvCache.setText(GlideCacheUtil.getInstance().getCacheSize(this));
    }

    private void showShare() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，微信、QQ和QQ空间等平台使用
        oks.setTitle("Demain");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("记录只有自己能看到的私密心情");
        oks.setImageUrl("https://www.bmob.cn/uploads/attached/app/logo/20180711/9ede3748-9162-09e3-6b97-10c6a39565fc.png");
        // url在微信、微博，Facebook等平台中使用
        oks.setUrl("https://www.pgyer.com/y7OM");
        // 启动分享GUI
        oks.show(this);
    }

    //flie：要删除的文件夹的所在位置
    private void deleteFile(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                deleteFile(f);
            }
//            file.delete();//如要保留文件夹，只删除文件，请注释这行
        } else if (file.exists()) {
            file.delete();
        }
    }

    @OnClick({R.id.iv_class_back, R.id.tv_modify, R.id.tv_setdrawer, R.id.rl_clear
            , R.id.tv_setrefresh, R.id.tv_opinion, R.id.tv_share, R.id.btn_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_class_back:
                finish();
                break;
            case R.id.tv_modify:
                startActivity(new Intent(this, ModifyActivity.class));
                break;
            case R.id.tv_setdrawer:
                startActivity(new Intent(this, DrawerToggleBackgroundActivity.class));
                break;
            case R.id.tv_setrefresh:
                updateRefreshFragment = new UpdateRefreshFragment();
                updateRefreshFragment.show(getSupportFragmentManager(), "tag");
                break;
            case R.id.tv_opinion:
                startActivity(new Intent(this, OpinionActivity.class));
                break;
            case R.id.rl_clear:
                GlideCacheUtil.getInstance().clearImageAllCache(this);
                deleteFile(new File(Environment.getExternalStorageDirectory() + "/demain/skin"));
                tvCache.setText("0M");
                break;
            case R.id.tv_share:
                showShare();
                break;
            case R.id.btn_out:
                EventBus.getDefault().post(new MessageEvent("更改密码"));
                finish();
                break;
        }
    }
}
